using System;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace UserManagementService.Users
{
    /// <summary>
    /// 用户组织关联实体
    /// </summary>
    public class UserOrganizationUnit : CreationAuditedEntity<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public virtual Guid UserId { get; protected set; }

        /// <summary>
        /// 组织ID
        /// </summary>
        public virtual Guid OrganizationId { get; protected set; }

        /// <summary>
        /// 组织名称（冗余字段，便于查询）
        /// </summary>
        public virtual string? OrganizationName { get; protected set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public virtual bool IsEnabled { get; protected set; }

        /// <summary>
        /// 加入时间
        /// </summary>
        public virtual DateTime JoinedAt { get; protected set; }

        /// <summary>
        /// 邀请人ID
        /// </summary>
        public virtual Guid? InvitedById { get; protected set; }

        /// <summary>
        /// 在组织中的职位
        /// </summary>
        public virtual string? Position { get; protected set; }

        /// <summary>
        /// 在组织中的级别
        /// </summary>
        public virtual int Level { get; protected set; }

        /// <summary>
        /// 是否为管理员
        /// </summary>
        public virtual bool IsManager { get; protected set; }

        /// <summary>
        /// 备注
        /// </summary>
        public virtual string? Remark { get; protected set; }

        protected UserOrganizationUnit()
        {
            IsEnabled = true;
            JoinedAt = Clock.Now;
            Level = 1; // 默认为普通成员级别
        }

        /// <summary>
        /// 创建用户组织关联
        /// </summary>
        public UserOrganizationUnit(
            Guid id,
            Guid userId,
            Guid organizationId,
            string? organizationName = null,
            Guid? invitedById = null,
            string? position = null,
            int level = 1,
            bool isManager = false,
            string? remark = null) : base(id)
        {
            UserId = userId;
            OrganizationId = organizationId;
            OrganizationName = organizationName;
            InvitedById = invitedById;
            Position = Check.Length(position, nameof(position), maxLength: 128);
            Level = level;
            IsManager = isManager;
            Remark = Check.Length(remark, nameof(remark), maxLength: 500);

            IsEnabled = true;
            JoinedAt = Clock.Now;
        }

        /// <summary>
        /// 启用关联
        /// </summary>
        public void Enable()
        {
            IsEnabled = true;
        }

        /// <summary>
        /// 禁用关联
        /// </summary>
        public void Disable()
        {
            IsEnabled = false;
        }

        /// <summary>
        /// 更新组织名称
        /// </summary>
        public void UpdateOrganizationName(string organizationName)
        {
            OrganizationName = Check.Length(organizationName, nameof(organizationName), maxLength: 256);
        }

        /// <summary>
        /// 更新职位
        /// </summary>
        public void UpdatePosition(string? position)
        {
            Position = Check.Length(position, nameof(position), maxLength: 128);
        }

        /// <summary>
        /// 更新级别
        /// </summary>
        public void UpdateLevel(int level)
        {
            if (level < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(level), "级别不能小于1");
            }
            Level = level;
        }

        /// <summary>
        /// 设置管理员状态
        /// </summary>
        public void SetManagerStatus(bool isManager)
        {
            IsManager = isManager;
        }

        /// <summary>
        /// 提升为管理员
        /// </summary>
        public void PromoteToManager()
        {
            IsManager = true;
            if (Level < 2)
            {
                Level = 2; // 管理员至少为2级
            }
        }

        /// <summary>
        /// 取消管理员权限
        /// </summary>
        public void DemoteFromManager()
        {
            IsManager = false;
        }

        /// <summary>
        /// 检查关联是否有效
        /// </summary>
        public bool IsValid()
        {
            return IsEnabled;
        }

        /// <summary>
        /// 检查是否为管理员
        /// </summary>
        public bool IsManagerRole()
        {
            return IsEnabled && IsManager;
        }
    }
}